package com.example.demohello.controller;

import com.example.demohello.entity.Todo;
import com.example.demohello.service.TodoService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TodoController {

  private final TodoService todoService;

  public TodoController(TodoService todoService) {
    this.todoService = todoService;
  }

  @PostMapping("/api/todos")
  public Todo createTodo(@RequestBody Todo todo) {
    return todoService.createOne(todo);
  }

  @GetMapping("/api/todos")
  public List<Todo> listTodos() {
    return todoService.listAll();
  }

  @PutMapping("/api/todos/{id}")
  public Todo updateById(@PathVariable(name = "id") Integer id,
                         @RequestBody Todo todo) {
    return todoService.updateById(id, todo);
  }

  @DeleteMapping("/api/todos/{id}")
  public void deleteTodo(@PathVariable(name = "id") Integer id) {
    todoService.removeById(id);
  }

  @GetMapping("/api/todos/{id}")
  public Todo getTodo(@PathVariable Integer id) {
    return todoService.getById(id);
  }
}
